package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.MemberElderDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.IDeviceDataService;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.DeviceDataGraphVo;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import com.zzyl.nursing.vo.LoginVo;
import com.zzyl.nursing.vo.MemberElderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 老人家属Controller
 * 
 * @author ruoyi
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属的接口")
public class FamilyMemberController extends BaseController
{
    @Autowired
    private IFamilyMemberService familyMemberService;

    @Autowired
    private IFamilyMemberElderService familyMemberElderService;

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IDeviceDataService deviceDataService;

    @PostMapping("/login")
    @ApiOperation("小程序登录")
    public AjaxResult login(@RequestBody UserLoginRequestDto dto){
        LoginVo loginVo = familyMemberService.login(dto);
        return success(loginVo);
    }

    /**
     * 1-绑定家人 （小组实战-01）
     *
     * @param memberElderDto 客户老人关联 DTO
     * @return 操作结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "绑定家人")
    public AjaxResult add(@RequestBody MemberElderDto memberElderDto) {
        return toAjax(familyMemberElderService.add(memberElderDto));
    }

    /**
     * 2-查询家人列表 （小组实战-02）
     *
     * @return 查询家人列表
     */
    @GetMapping("/my")
    @ApiOperation(value = "查询家人列表")
    public R<List<FamilyMemberElderVo>> my() {
        List<FamilyMemberElderVo> memberElders = familyMemberElderService.my();
        return R.ok(memberElders);
    }

    /**
     * 3-查看已绑定家人列表记录（分页查询） （小组实战-03）
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return 家人列表
     */
    @GetMapping("/list-by-page")
    @ApiOperation(value = "查看已绑定家人列表记录（分页查询）")
    public R<List<MemberElderVo>> listByPage(Integer pageNum, Integer pageSize) {
        startPage();
        Long userId = UserThreadLocal.getUserId();
        List<MemberElderVo> memberElders = familyMemberElderService.listByPage(userId);
        return R.ok(memberElders);
    }



    /**
     * 4-查询健康数据 （小组实战-04）
     *
     * @param iotId 设备ID
     * @return 健康数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation(value = "查询健康数据")
    public R<List> queryDevicePropertyStatus(@PathVariable("iotId") String iotId){
        return R.ok(deviceService.queryServiceProperties(iotId)) ;
    }

    /**
     * 5-按天统计查询指标数据 （小组实战-05）
     *
     * @param iotId 设备ID
     * @param functionId 指标ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 指标数据
     */
    @GetMapping("/queryDeviceDataListByDay")
    @ApiOperation(value = "按天统计查询指标数据")
    public AjaxResult queryDeviceDataListByDay(
            String iotId,String functionId,Long startTime,Long endTime
    ){
        List<DeviceDataGraphVo> list = deviceDataService.queryDeviceDataListByDay(iotId,functionId,startTime,endTime);
        return success(list);
    }

    /**
     * 6-按周统计查询指标数据 （小组实战-06）
     *
     * @param iotId 设备ID
     * @param functionId 指标ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 指标数据
     */
    @GetMapping("/queryDeviceDataListByWeek")
    @ApiOperation(value = "按周统计查询指标数据")
    public AjaxResult queryDeviceDataListByWeek(
            String iotId,String functionId,Long startTime,Long endTime
    ){
        List<DeviceDataGraphVo> list = deviceDataService.queryDeviceDataListByWeek(iotId,functionId,startTime,endTime);
        return success(list);
    }
}
